package ct.zxd.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class OrderDao {
	
	public int insertOrder(long time, float money, String orderList, String address,
			String shop_id, String ordertype, String customer) throws SQLException{
					Connection conn=null;
					int result=0;	
					conn=DBconn.getconn();//调用例2-1创建的连接类中静态方法
					if(conn!=null){		
					String sql="insert into ordertable(time,money," +
							"list,address,shop_id,type,customer) " +
							"values(?,?,?,?,?,?,?)";//带参数SQL语句
						PreparedStatement pstm=conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
						pstm.setLong(1, time);
						pstm.setFloat(2, money);
						pstm.setString(3, orderList);
						pstm.setString(4, address);
						pstm.setString(5, shop_id);
						pstm.setString(6, ordertype);
						pstm.setString(7, customer);
						int count=pstm.executeUpdate();//执行更新操作
						if (count > 0) {
							ResultSet rs = pstm.getGeneratedKeys();
							if (rs.next()) {
								result = rs.getInt(1);
							}
						}
						
					    DBconn.closeconn(conn);//关闭连接
					}
					
					return result;
				}
	
	public int deleteOrder(int orderid,String shopid) throws SQLException{
		Connection conn=null;
		int result=0;
		conn=DBconn.getconn();
		if(conn!=null){		
				String sql="delete from ordertable where id = ? and shop_id=?";
				PreparedStatement pstm=conn.prepareStatement(sql);
				pstm.setInt(1, orderid);
				pstm.setString(2, shopid);
				result=pstm.executeUpdate();//执行更新操作
				DBconn.closeconn(conn);//关闭连接
		}		
		return result;
	  }
	
	public int updateOrderLogistics(int orderid, String shop, String ship, String kd) throws SQLException{
		Connection conn=null;
		int result=0;	
		conn=DBconn.getconn();
		if(conn!=null){		
				String sql="update ordertable set shipper=?, kdcode=? where id=? and shop_id=?";
				PreparedStatement pstm=conn.prepareStatement(sql);
				pstm.setString(1, ship);
				pstm.setString(2, kd);
				pstm.setInt(3, orderid);
				pstm.setString(4, shop);
				result=pstm.executeUpdate();//执行更新操作
				DBconn.closeconn(conn);//关闭连接
		}		
		return result;
	}
	
	public int updateOrderStatus(int orderid, String shop, int status) throws SQLException{
		Connection conn=null;
		int result=0;	
		conn=DBconn.getconn();
		if(conn!=null){		
				String sql="update ordertable set status=? where id=? and shop_id=?";
				PreparedStatement pstm=conn.prepareStatement(sql);
				pstm.setInt(1, status);
				pstm.setInt(2, orderid);
				pstm.setString(3, shop);
				result=pstm.executeUpdate();//执行更新操作
				DBconn.closeconn(conn);//关闭连接
		}		
		return result;
	}

}
